package oa.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import oa.web.pojo.Employee;

import com.ndktools.javamd5.Mademd5;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.web.context.ContextLoader;

import java.io.InputStream;
import java.util.Properties;

public class Constant {


    //set some cookie status
    public static final int COOKIE_EXPIRE = -1;

	//set some cookie properties
	public static final int COOKIE_MAX_AGE = 24*60;
    public static final String COOKIE_DOMAIN = "minioa.com";
    public static final String COOKIE_PATH="/";
    public static final int COOKIE_TRANSIENT_AGE = -1;
    public static final int COOKIE_NULL_AGE = 0;

    public static  String LOGIN_COOKIE_NAME = "Login";
    public static  String NEXTURL_COOKIE_NAME= "NextUrl";

    //set login type, 0:cookie login, 1:username,password
    public static final int COOKIE_LOGIN = 0;
    public static final int USERNAME_PASS_LOGIN = 1;

    //set the redis expire time(milli seconds)
	public static int EXPIRE=60*60*1000;

	//set log status
    public static final int LOGIN_STATUS_INIT = -1;
	public static final int  LOGIN_STATUS_ERRORPASSWORD = 0;
	public static final int LOGIN_STATUS_ERRORUSERNAME = 1;
	public static final int LOGIN_STATUS_LOGIN_SUCCESS = 2;
	
	//set session attributes
	public static String CURRENT_USER = "CURRENT_USER";
	
	//attend type
	public static final int ZHENGCHANG = 1;
	public static final int SHIJIA = 1;
	public static final int BINGJAI = 1;
	public static final int CHIDAO = 1;
	public static final int ZAOTUI = 1;
	public static final int KUANGGONG = 1;
	public static final int CHUCHAI = 1;


    //encode pattern

    private static  Mademd5 MD5 ;
    private static Base64 BASE64;
	
	public static Mademd5 getMD5Instance(){

        if(MD5 == null){
            return new Mademd5();
        }
        return MD5;
	}

	public static Base64 getBASE64Instance(){
        if(BASE64 == null){
            return new Base64();
        }
        return BASE64;
    }


    public static String encodeMD5(String str){
        return getMD5Instance().toMd5(str);
    }

    public static String encodeBASE64(String str){
        byte b[] = getBASE64Instance().encode(StringUtils.getBytesUtf8(str));
        return StringUtils.newStringUtf8(b);
    }

    public static String decodeBASE64(String str){
        byte b[] = getBASE64Instance().decode(StringUtils.getBytesUtf8(str));
        return StringUtils.newStringUtf8(b);
    }

	public static Employee getEmp(HttpServletRequest req) {
		HttpSession session = MySession.getInstance(req);
		Employee emp = null;
		try {
			emp = (Employee) session.getAttribute(Constant.CURRENT_USER);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return emp;
	}

    public static void main(String args[]){
        String str = "hello world";
        System.out.println(Constant.encodeBASE64(str));
        String token = Constant.encodeBASE64(str);
        System.out.println(Constant.decodeBASE64(token));
    }
}
